<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('还款日期')" prop="repaymentDate">
            <el-date-picker
              v-model="form.repaymentDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('还款日期')"
              :disabled="isRepayment === 'repayment'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('应还本金（元）')" prop="principalPayable">
            <Money v-model="form.principalPayable" :placeholder="$t('应还本金（元）')" :disabled="isRepayment === 'repayment'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('应还利息（元）')" prop="interestPayable">
            <Money v-model="form.interestPayable" :placeholder="$t('应还利息（元）')" :disabled="isRepayment === 'repayment'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('应付手续费（元）')" prop="serviceChargesPayable">
            <Money v-model="form.serviceChargesPayable" :placeholder="$t('应付手续费（元）')" :disabled="isRepayment === 'repayment'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本次应付总额（元）')" prop="totalAmountPayable">
            <Money v-model="form.totalAmountPayable" :placeholder="$t('本次应付总额（元）')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('当前余额（元）')" prop="currentBalance">
            <Money v-model="form.currentBalance" :placeholder="$t('当前余额（元）')" :disabled="isRepayment === 'repayment'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isRepayment === 'repayment'" :label="$t('实际还款日期')" prop="actualRepaymentDate">
            <el-date-picker
              v-model="form.actualRepaymentDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('实际还款日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isRepayment === 'repayment'" :label="$t('实际支付本金（元）')" prop="actualPrincipalPaid">
            <Money v-model="form.actualPrincipalPaid" :placeholder="$t('实际支付本金（元）')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isRepayment === 'repayment'" :label="$t('实际支付利息（元）')" prop="actualInterestPaid">
            <Money v-model="form.actualInterestPaid" :placeholder="$t('实际支付利息（元）')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isRepayment === 'repayment'" :label="$t('实际支付手续费（元）')" prop="actualServiceChargePaid">
            <Money v-model="form.actualServiceChargePaid" :placeholder="$t('实际支付手续费（元）')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isRepayment === 'repayment'" :label="$t('本次实际支付总额（元）')" prop="totalAmountActuallyPaid">
            <Money v-model="form.totalAmountActuallyPaid" :placeholder="$t('本次实际支付总额（元）')" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="financing:repayment" />
    </div>
  </div>
</template>

<script>
import {
  saveDealFinancingRepayment,
  getDealFinancingRepayment,
  totalAmountPayableCalculate, totalAmountActuallyPaidCalculate
} from '@/api/deal/financing/repayment/dealFinancingRepayment'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 修改类型
                isRepayment: undefined,
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  repaymentDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  actualRepaymentDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                }
            }
        },
        watch: {
          'form.principalPayable': {
            handler() {
                this.totalAmountPayableCalculate(this.form)
            },
            immediate: true
          },
          'form.interestPayable': {
            handler() {
                this.totalAmountPayableCalculate(this.form)
            },
            immediate: true
          },
          'form.serviceChargesPayable': {
            handler() {
                this.totalAmountPayableCalculate(this.form)
            },
            immediate: true
          },
          'form.actualPrincipalPaid': {
            handler() {
                this.totalAmountActuallyPaidCalculate(this.form)
            },
            immediate: true
          },
          'form.actualInterestPaid': {
            handler() {
                this.totalAmountActuallyPaidCalculate(this.form)
            },
            immediate: true
          },
          'form.actualServiceChargePaid': {
            handler() {
                this.totalAmountActuallyPaidCalculate(this.form)
            },
            immediate: true
          }

        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data, type) {
                this.isRepayment = type
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealFinancingRepayment(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    repaymentDate: undefined,
                    principalPayable: undefined,
                    interestPayable: undefined,
                    serviceChargesPayable: undefined,
                    totalAmountPayable: undefined,
                    currentBalance: undefined,
                    actualRepaymentDate: undefined,
                    actualPrincipalPaid: undefined,
                    actualInterestPaid: undefined,
                    actualServiceChargePaid: undefined,
                    totalAmountActuallyPaid: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealFinancingRepayment(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 计算本次应付总额
          totalAmountPayableCalculate(data) {
            totalAmountPayableCalculate(data).then(response => {
              this.form.totalAmountPayable = response.data.totalAmountPayable
            })
          },
          // 计算本次实际支付总额
          totalAmountActuallyPaidCalculate(data) {
            totalAmountActuallyPaidCalculate(data).then(response => {
              this.form.totalAmountActuallyPaid = response.data.totalAmountActuallyPaid
            })
          }
        }
    }
</script>
